//
//  FKOrderListCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/22.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKOrderListCell.h"
#import "FKOrderGoods.h"
#import "FKOperationBtnStatus.h"

@interface FKOrderListCell ()

@property (nonatomic, weak) UIImageView *pictureView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *subTitleLabel;
/** 预约剩余时间 */
@property (nonatomic, weak) UILabel *restTimeLabel;
@property (nonatomic, weak) UILabel *elecCodeLabel;
@property (nonatomic, weak) UILabel *numLabel;
//@property (nonatomic, weak) UIButton *refundBtn;
@property (nonatomic, strong) NSArray *btns;
@property (nonatomic, assign) NSInteger countDown;

@end

@implementation FKOrderListCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateReserveTime) name:@"updateReserveTime" object:nil];
    
    UIImageView *pictureView = [[UIImageView alloc] init];
    [self.contentView addSubview:pictureView];
    self.pictureView = pictureView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    titleLabel.numberOfLines = 2;
    
    UILabel *subTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:subTitleLabel];
    self.subTitleLabel = subTitleLabel;
    
    UILabel *elecCodeLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkMainColor text:@"电子码商品不支持退款哦!"];
    [self.contentView addSubview:elecCodeLabel];
    self.elecCodeLabel = elecCodeLabel;
    
    UILabel *restTimeLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkMainColor];
    [self.contentView addSubview:restTimeLabel];
    self.restTimeLabel = restTimeLabel;
    restTimeLabel.hidden = true;
    
    UILabel *numLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:numLabel];
    self.numLabel = numLabel;
    
    NSMutableArray *itemBtns = [NSMutableArray array];
    UIButton *lastBtn = nil;
    CGFloat itemW = fk_adjustW(60);
    CGFloat itemH = fk_adjustW(20);
    CGFloat margin = 8;
    CGFloat itemX = fkScreenW - 12 - itemW;
    CGFloat itemY = fk_adjustW(120) - fk_adjustW(12) - itemH;
    for (NSInteger index = 0; index < 3; index++) {
        UIButton *itemBtn = [UIButton fk_btnWithTarget:self action:@selector(itemBtnClicked:) font:fk_adjustFont(12) normalTitle:nil normalTitleColor:nil selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
        [self.contentView addSubview:itemBtn];
        [itemBtns addObject:itemBtn];
        [itemBtn setTitleColor:fkColor999999 forState:UIControlStateDisabled];
        [itemBtn fk_viewCornerRadius:fk_adjustW(10) borderWidth:0.8 borderColor:fkColor666666];
        itemBtn.hidden = true;
        lastBtn = itemBtn;
        
        if (index > 0) {
            itemX -= (margin + itemW);
        }
        itemBtn.frame = CGRectMake(itemX, itemY, itemW, itemH);
    }
    self.btns = itemBtns;
    
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.offset(fk_adjustW(78));
        make.width.height.offset(fk_adjustW(100));
        make.centerY.offset(0);
        make.left.offset(12);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pictureView.mas_right).offset(8);
        make.right.offset(-12);
        make.top.equalTo(pictureView).offset(4);
    }];
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.right.offset(-30);
//        make.bottom.equalTo(pictureView).offset(fk_adjustW(-22));
        make.top.equalTo(titleLabel.mas_bottom).offset(fk_adjustW(6));
    }];
    [restTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(subTitleLabel);
        make.bottom.equalTo(pictureView);
    }];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleLabel);
//        make.centerY.equalTo(subTitleLabel);
        make.bottom.equalTo(pictureView);
    }];
    [elecCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.equalTo(numLabel.mas_top).offset(fk_adjustW(-6));
    }];
}

- (void)updateReserveTime
{
    if ([self.data isKindOfClass:[FKOrderGoods class]]) {
        if (self.countDown > 0) {
            self.countDown--;
            NSInteger days = self.countDown / 86400;
            NSInteger hours = self.countDown % 86400 / 3600;
            NSInteger minutes = self.countDown % 3600 / 60;
//            NSInteger seconds = self.countDown % 60;
            self.restTimeLabel.text = fkFORMAT(@"距离预约时间还剩:%02zd天%02zd时%02zd分", days, hours, minutes);
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)itemBtnClicked:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(orderListCell:didClickedItemBtn:title:)]) {
        FKOrderGoods *goods = (FKOrderGoods *)self.data;
        [self.delegate orderListCell:self didClickedItemBtn:goods title:btn.titleLabel.text];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    self.restTimeLabel.hidden = true;
    self.elecCodeLabel.hidden = true;
    if ([data isKindOfClass:[FKOrderGoods class]]) {
        FKOrderGoods *goods = (FKOrderGoods *)data;
        [self.pictureView sd_setImageWithURL:[NSURL URLWithString:goods.good_img] placeholderImage:kPlaceholder_1x1];
        self.titleLabel.text = goods.good_title;
        self.numLabel.text = fkFORMAT(@"x%@", goods.num);
        self.subTitleLabel.text = goods.option_title;
        
        self.numLabel.hidden = goods.btnStatuses.count > 0;
        for (NSInteger index = 0; index < self.btns.count; index++) {
            UIButton *itemBtn = self.btns[index];
            if (index < goods.btnStatuses.count) {
                FKOperationBtnStatus *status = goods.btnStatuses[index];
                itemBtn.hidden = false;
                [itemBtn setTitle:status.title forState:UIControlStateNormal];
                [itemBtn setTitleColor:status.themeColor forState:UIControlStateNormal];
                itemBtn.layer.borderColor = status.themeColor.CGColor;
                
                itemBtn.enabled = !status.disable;
                if (!itemBtn.isEnabled) itemBtn.layer.borderColor = fkColor999999.CGColor;
            } else {
                itemBtn.hidden = true;
            }
        }
        
        if (goods.reserve_day) {
            self.countDown = [goods.reserve_day integerValue];
            self.restTimeLabel.hidden = false;
        }
        if ([goods.is_elec_code boolValue]) {
            self.elecCodeLabel.hidden = false;
        }
    } else if ([data isKindOfClass:[NSDictionary class]]) {
        [self.pictureView sd_setImageWithURL:[NSURL URLWithString:data[@"image"]] placeholderImage:kPlaceholder_1x1];
        self.titleLabel.text = data[@"title"];
        self.numLabel.text = fkFORMAT(@"x%@", data[@"num"]);
        self.subTitleLabel.text = data[@"option_title"];
    }
    
//    self.titleLabel.text = @"测试\n测试";
//    self.subTitleLabel.text = @"规格";
//    self.restTimeLabel.text = @"--剩余时间---";
//    self.restTimeLabel.hidden = false;
//    self.elecCodeLabel.hidden = false;
}

@end
